package com.bdqn.controller;


import com.bdqn.pojo.User;
import com.bdqn.service.UserService;
import com.bdqn.utils.Result;
import com.bdqn.utils.UserHolder;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;


import java.util.UUID;

@RestController
@RequestMapping("/admin")
public class LoginController {

    @Autowired
    private UserService service;

    @RequestMapping("/login")
    public Result login(@RequestBody User user) {
        user = service.login(user);
        if (user == null) {
            return Result.fail("登录失败,账号或密码错误!");
        }
        String token = System.currentTimeMillis() + UUID.randomUUID().toString() + "-" + user.getId();
        return Result.ok("登录成功!", token);
    }

    @RequestMapping("/logout")
    public Result logout() {
        UserHolder.remove();
        return Result.ok("注销成功!");
    }

}
